/*本模块功能:我的信息(管理员)
        编写者:朱光曜
        编写时间:2017.11.20*/
$(function() {
    var box = new Alert;


    function myMessage(message) {
        //我的信息

        if(message.memberList[0].sexName == "男") {
            var sexList = '<input type="radio" checked="checked" name="sex" id="man" value=""/><label for="man" class="checked" userSex="m">男</label> <input type="radio" name="sex" id="woman" value=""/><label for="woman" userSex="f">女</label>';
        }
        if(message.memberList[0].sexName == "女") {
            var sexList = '<input type="radio"  name="sex" id="man" value=""/><label for="man" userSex="m">男</label> <input type="radio" checked="checked"  name="sex" id="woman" value=""/><label for="woman" class="checked" userSex="f">女</label>';
        }
        return '<div class="fl inputRight"><label for="departName" class="fl">工&ensp;&ensp;&ensp;&ensp;号:&ensp;</label><div id="studentID"   class="fl" title="' + message.memberList[0].stuorteaId + '">' + message.memberList[0].stuorteaId + '</div></div>' +
            '<div class="fr inputRight"><label for="departName" class="fl">姓&ensp;&ensp;&ensp;&ensp;名:&ensp;</label><div id="studentID"   class="fl" title="' + message.memberList[0].userName + '">' + message.memberList[0].userName + '</div></div>' +
            '<div class="fl inputRight"><label for="departName" class="fl" departmentId="' + message.memberList[0].departmentId + '">所属部门:&ensp;</label><div id="department"   class="fl" title="' + message.memberList[0].depName + '">' + message.memberList[0].depName + '</div></div>' +
            '<div class="fr inputRight"><label for="departName">联系电话:&ensp;</label><input autocomplete="off" type="text" id="phone" class="publicText" value="' + message.memberList[0].telephoneNum + '"/></div>' +
            '<div class="fl sex inputRight" id="sex"><span>性&ensp;&ensp;&ensp;&ensp;别:&ensp;</span>' +
            sexList +
            '</div>' +
            '<div class="clear"></div>';
    }

    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });


    var nowData = 1;
    var index = {

        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            this.data_content = {}; //搜索集
            this.tab_content = {}; //列表集
            this.parameter = {
                "pageNow": 1,
                "pageSize": 10,
                "param": 1,
                "roleId":3,
                "selecetRoleId":3,
                "stuorteaId":result.stuorteaId
            };

            this.postData(this.parameter, nowData);

            $(document).off("click", "#sex input").on("click", "#sex input", function() {

                $(this).next("label").addClass("checked").siblings("label").removeClass("checked");
            });

        },
        add: function() { //搜索追加
            var self = this;
            var data = self.data_content;

            $(".funBox").html(myMessage(data));
            $(".publicText").bind('input propertychange',function(){

                var regy = /\s/g;
                if(regy.test($(this).val())){
                    $(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
                }
            });
            $(".searchBtn").off("click").click(function() { //搜索点击
                self.keep();
            });

        },
        postData: function(par_data, nowData,hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;

            $.ajax({
                type: "post",
                url: "getMemberDetailById",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    var new_data = JSON.parse(data);
                    self.data_content = new_data;
                    self.tab_content = new_data;
                    self.add();



                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        keep: function() {//保存信息
            var self = this;
            var telephoneNum = $("#phone").val();
            var userSex = $(".checked").attr("usersex");
            var n = /^[1-9]\d*$|^0$/;

            // if(!n.test(telephoneNum)&&telephoneNum!=="") {
            //     self.alert1("请输入数字信息");
            //     return;
            // } else if(telephoneNum.length !== 11&&telephoneNum.length !== 0) {
            //     self.alert1("请输入11位数字信息");
            //     return;
            // }else if(verification.telphoneCode(telephoneNum) !== true) {
            //     self.alert1(verification.telphoneCode(telephoneNum));
            //     return;
            // }
            var thisData = {
                "param": 3,
                "telephoneNum": telephoneNum,
                "userSex": userSex,
                "userContect": "",
            };

            $.ajax({
                type: "post",
                url: "saveMyInfo",
                async: true,

                data: {
                    "jsonStr": JSON.stringify(thisData)
                },
                async: true,
                success: function(msg) {
                    var msgNew = JSON.parse(msg);

                    if(msgNew.flag) {
                        self.alert1("保存成功");
                    } else {
                        //self.alert1(msgNew.msg);
                        if(msgNew.msg){
                            self.alert1(msgNew.msg);
                        }else{
                            self.alert1("保存失败");
                        }

                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        alert1: function(content2, method) { //alert提示

            box.contenthead = "提示";
            box.content = content2;
            if(method) {
                box.success = method;
            }
            box.init();
            box.show();
        }

    };

    index.init();

});